﻿//-----------------------------------------------------------------------------------
// Detailed Extraction Rule Template version 1.0
//-----------------------------------------------------------------------------------
using System;
using System.Diagnostics;
using System.ComponentModel;
using System.Globalization;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Xml;
using System.Xml.XPath;
using System.Collections;
namespace WebServicesTesting.Automation.WebTest.ExtractionRules
{
    [System.ComponentModel.DisplayName("RowCount")]
    [System.ComponentModel.Description("TODO: Add a more detailed description of RowCount extraction rule.")]
    public class RowCountExtraction : ExtractionRule
    {
        #region Constructors
        /// <summary>
        /// standard constructor
        /// </summary>
        public RowCountExtraction() { }
        #endregion

        #region Properties
        /// <summary>
        /// If True, the extraction rule fails if not found.
        /// </summary>
       

        /// <summary>
        /// Contains a message that is surfaced to the user
        /// </summary>
        private string xPathExpression;
        [Description("XPath Expression to evaluate against the response")]
        public string XPathExpression
        {
            get { return xPathExpression; }
            set { xPathExpression = value; }
        }
        private string contextParam;
        public string ContextParam
        {
            get { return contextParam; }
            set { contextParam = value; }
        } 

        /*
         * Public properties on the class will be surfaced to the user in the VS UI.
         * Only string, int, and bool types are allowed.
         */

        /*
            /// <summary>
            /// Sample string property
            /// </summary>
            private string m_myStringProperty;
            [System.ComponentModel.DisplayName("My String Property")]
            [System.ComponentModel.Description("Enter a long description for this property.")]
            [System.ComponentModel.DefaultValue("MyString")]
            public string MyStringProperty
            {
                get { return m_myStringProperty; }
                set { m_myStringProperty = value; }
            }
        */

        #endregion // Properties

        #region Utility Functions
        /// <summary>
        /// A sample utility function that performs the actual work of extracting values. 
        /// This demonstrates the best practice of moving most of the code out into 
        /// it's own function so you can test it from a unit test.
        /// </summary>
        /// <returns>true - value found, false - value not found, reason contains more details</returns>
        public bool Extract(string document)
        {
            Debug.Assert(document != null);
            // TODO:  Implement your extraction code here
            throw (new NotImplementedException("ExtractionRule1:MyExtractionFunction not implemented !"));

            // m_extractedValue = "Value found";
            // m_message = "Additional information regarding success/failure of function";
            // return true; // true if found, false if not found
        }
        #endregion


                   


        #region Extract Method
        /// <summary>
        /// The Extract method.  The parameter e contains the Web test context.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void Extract(object sender, ExtractionEventArgs e)
        {
           
            try
            {
                
                XmlDocument doc = e.Response.XmlDocument;

                XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
                nsMgr.AddNamespace("s", doc.DocumentElement.NamespaceURI);
                XPathNavigator nav = doc.CreateNavigator();
                XmlNode responseBody = doc.SelectSingleNode("//s:Body", nsMgr);
                string text = null;
                while (responseBody.HasChildNodes)
                {
                    text = responseBody.Name;
                    if (text == xPathExpression)
                    {
                        break;
                    }
                    else
                    {
                        responseBody = responseBody.LastChild;
                    }

                }

                XmlNodeList node = responseBody.ChildNodes;
                int count = node.Count;
                e.WebTest.Context[ContextParam] = count;
            }

            catch (Exception ex)
            {
                e.Message = String.Format(CultureInfo.CurrentCulture, "{0}[{1}] Exception: {2}", "ExtractionRule1", this.ContextParameterName, ex.Message.ToString());
                e.Success = false;
            }
           
        }
       


         
             #endregion
    }
}
